//
//  WQUserAllDeviceViewController.m
//  WisdomQuestor
//
//  Created by 纪景城 on 2024/12/23.
//

#import "WQUserAllDeviceViewController.h"
#import <ThingSmartBizCore/ThingSmartBizCore.h>

@interface WQUserAllDeviceViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation WQUserAllDeviceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"所有设备";
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((kScreen_Width - kFragmemtFloat(33)) / 2, kFragmemtFloat(150));
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.backgroundColor = UIColor.clearColor;
    
    [self.collectionView registerClass:[WQDeviceStatusCollectionCell class] forCellWithReuseIdentifier:[WQDeviceStatusCollectionCell kj_runtimeClassName]];
    
    [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.view);
        make.left.equalTo(self.view.mas_left).offset(kFragmemtFloat(10));
        make.right.equalTo(self.view.mas_right).offset(-kFragmemtFloat(10));
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
    
    
    [self collBeginRefresh];
    
//    [[ThingSmartBizCore sharedInstance] registerService:@protocol(ThingSmartHomeDataProtocol) withInstance:self];
//    [[ThingSmartBizCore sharedInstance] registerService:@protocol(ThingSmartHouseIndexProtocol) withInstance:self];
}

//- (ThingSmartHome *)getCurrentHome {
//    ThingSmartHome *home = [ThingSmartHome homeWithHomeId:WQShare.userHomeModel.homeId];
//    return home;
//}
//- (BOOL)homeAdminValidation {
//    return YES;
//}

#pragma mark ------- Http Mehods -------

- (void)requestHandlerWishIsRefresh:(BOOL)isRefresh {
    DEBUGLOG(@"deviceList - %@", [WQShare.userHomeModel.deviceList yy_modelToJSONString]);
    self.collectionData = WQShare.userHomeModel.deviceList;
    [self collEndRefresh];
}

// 添加设备到房间
- (void)addDeviceToRoomWithDeivce:(ThingSmartDeviceModel *)deviceModel room:(ThingSmartRoomModel *)roomModel {
    
    ThingSmartRoom *room = [[ThingSmartRoom alloc] initWithRoomId:roomModel.roomId homeId:WQShare.userHomeModel.homeId];
    
    
    kWEAKSELF
    [room addDeviceWithDeviceId:deviceModel.devId success:^{
        DEBUGLOG(@"房间添加设备成功");
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationRoomDeviceUpdate object:nil];
        [weakSelf.collectionView reloadData];
        
    } failure:^(NSError *error) {
        DEBUGLOG(@"房间添加设备失败");
    }];
}

#pragma mark ------- UICollectionViewDelegate -------

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.collectionData.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WQDeviceStatusCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[WQDeviceStatusCollectionCell kj_runtimeClassName] forIndexPath:indexPath];
    
    ThingSmartDeviceModel *deviceModel = self.collectionData[indexPath.row];
    
    [cell updateUIWithModel:deviceModel];
    
    [cell kj_AddGestureRecognizer:KJGestureTypeDouble block:^(UIView * _Nonnull view, UIGestureRecognizer * _Nonnull gesture) {
        
        kWEAKSELF
        [TSHandleAlertView pad_showSystemSheetViewWithTitleArray:@[@"添加至首页", @"管理设备", @"修改房间", @"删除此设备"] confirm:^(NSInteger buttonTag) {
            
            switch (buttonTag) {
                case 2:
                {
                    // 修改房间
                    WQChoiseRoomListController *vc = [[WQChoiseRoomListController alloc] init];
                    vc.choiseRoomTemplateBlock = ^(BOOL isDone, ThingSmartRoomModel * _Nullable roomModel) {
                        if (isDone == YES) {
                            [weakSelf addDeviceToRoomWithDeivce:deviceModel room:roomModel];
                        }
                    };
                    [weakSelf.navigationController presentViewController:[[OCNavigationViewController alloc] initWithRootViewController:vc] animated:YES completion:nil];
                }
                    break;
                    
                default:
                    break;
            }
            

        } view:[UIApplication currentRootTopViewController].view frame:CGRectZero];
        
    }];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ThingSmartDeviceModel *deviceModel = self.collectionData[indexPath.row];
    
    id<ThingPanelProtocol> impl = [[ThingSmartBizCore sharedInstance] serviceOfProtocol:@protocol(ThingPanelProtocol)];
    [impl gotoPanelViewControllerWithDevice:deviceModel group:nil initialProps:nil contextProps:nil completion:^(NSError * _Nullable error) {
        
    }];
}

@end
